package com.vf.cloud.paas.app.master.api.scene.controller;

import java.util.List;

import org.apache.commons.text.StringEscapeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.kit.JsonKit;
import com.jfinal.kit.StrKit;
import com.vf.cloud.paas.app.master.api.scene.service.ISceneLayer;
import com.vf.cloud.paas.app.master.common.domain.SceneActor;
import com.vf.cloud.paas.app.master.common.domain.SceneLayer;
import com.vf.cloud.paas.app.master.common.vo.scene.SceneActorPageP;
import com.vf.cloud.paas.app.master.common.vo.scene.SceneActorParam;
import com.vf.cloud.paas.app.master.common.vo.scene.SceneTree;
import com.vf.cloud.paas.app.master.common.vo.scene.SceneTreeParam;
import com.vf.cloud.paas.common.util.R;
import com.vf.cloud.paas.common.util.UuidUtil;

import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
@RestController
@RequestMapping("/api/v1/scene/actor")
public class SceneActorController {

	@Autowired
	private ISceneLayer sceneLayerImpl;

	@RequestMapping(value = "/list", method = RequestMethod.POST)
	public R<List<SceneActor>> list(@RequestBody SceneActorPageP param) throws Exception {

		String sqlExceptSelect = " FROM " + SceneActor.TABLE_NAME + " o  WHERE 1=1 ";
		if (!StrKit.isBlank(param.getKeywords())) {
			sqlExceptSelect += " AND ( O.NAME LIKE '%" + param.getKeywords() + "%' ) ";
		}
		sqlExceptSelect += "  ORDER BY   O.MODIFY_TIME desc ";
		List<SceneActor> sceneActors = SceneActor.dao.find("select *  " + sqlExceptSelect);
		return R.ok(sceneActors);
	}

	@RequestMapping(value = "/addLayer", method = RequestMethod.POST)
	public R<List<SceneTree>> addLayer(@RequestBody SceneTreeParam param) {

		SceneLayer sceneLayer = new SceneLayer();
		sceneLayer.setId(UuidUtil.getUUID());
		sceneLayer.setLabel("新增文件夹");
		sceneLayer.setPid(StrKit.isBlank(param.getPid()) ? "-1" : param.getPid());
		sceneLayer.save();
		return R.ok(sceneLayerImpl.getTree(param));
	}

	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public R<SceneActor> update(@RequestBody SceneActorParam param) {
		if (param == null) {
			return R.failed("参数必填.");
		}

		SceneActor sceneActor = SceneActor.dao.findById(param.getId());
		if (sceneActor == null) {
			return R.failed("无效参数.");
		}
		if (!StrKit.isBlank(param.getName()))
			sceneActor.setName(param.getName());

		if (!StrKit.isBlank(param.getLayer_id())) {
			sceneActor.setLayerId(param.getLayer_id());
		}

		if (!StrKit.isBlank(param.getPosition())) {
			sceneActor.setPosition(param.getPosition());
		}
		if (!StrKit.isBlank(param.getRotation())) {
			sceneActor.setRotation(param.getRotation());
		}
		if (!StrKit.isBlank(param.getScale())) {
			sceneActor.setScale(sceneActor.getScale());
		}

		if (!StrKit.isBlank(param.getNormalForLabel()) || !StrKit.isBlank(param.getActivateForLabel())
				|| !StrKit.isBlank(param.getNormalForImg()) || !StrKit.isBlank(param.getActivateForImg())) {

			String stlye = sceneActor.getStyle();
			
			//JSONObject.parseObject(text)

			JSONObject styleObj;

			if (StrKit.isBlank(stlye)) {
				styleObj = new JSONObject();
			} else {
				styleObj = JSONObject.parseObject(stlye);
				if (styleObj == null)
					styleObj = new JSONObject();
			}

			if (!StrKit.isBlank(param.getNormalForLabel()) && !StrKit.isBlank(param.getActivateForLabel())) {
				JSONObject labelObj = new JSONObject();
				labelObj.put("normal", JSONObject.parseObject(StringEscapeUtils.unescapeJava(param.getNormalForLabel())) );
				labelObj.put("activate",JSONObject.parseObject(StringEscapeUtils.unescapeJava(param.getActivateForLabel())));
				styleObj.put("label", labelObj);
			}
			if (!StrKit.isBlank(param.getNormalForImg()) && !StrKit.isBlank(param.getActivateForImg())) {
				JSONObject markerObj = new JSONObject();
				markerObj.put("normal", JSONObject.parseObject(StringEscapeUtils.unescapeJava(param.getNormalForImg())));
				markerObj.put("activate", JSONObject.parseObject(StringEscapeUtils.unescapeJava(param.getActivateForImg())));
				styleObj.put("marker", markerObj);
			}
			sceneActor.setStyle(JsonKit.toJson(styleObj));
		}

		if (!sceneActor.update()) {
			return R.failed("更新失败.");
		}
		return R.ok(sceneActor);
	}

	@RequestMapping(value = "/getById", method = RequestMethod.POST)
	public R<SceneActor> getById(@RequestBody SceneActor param) {
		if (param == null) {
			return R.failed("参数必填.");
		}
		SceneActor sceneActor = SceneActor.dao.findById(param.getId());
		if (sceneActor == null) {
			return R.failed("无效参数.");
		}
		return R.ok(sceneActor);
	}

	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public R<SceneActor> delete(@RequestBody SceneActor param) {
		if (param == null) {
			return R.failed("参数必填.");
		}

		SceneActor sceneActor = SceneActor.dao.findById(param.getId());
		if (sceneActor == null) {
			return R.failed("无效参数.");
		}
		if (!sceneActor.delete()) {
			return R.failed("删除失败.");
		}
		return R.ok(sceneActor);
	}
}
